package com.hiv.mapper;

import com.hiv.mapper.domain.Permission;
import com.hiv.mapper.domain.RolePermission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * role-permisson
 */
@Mapper
@Repository
public interface RolePermissionMapper {
    /** 增(批量插入) */
    @Select({"<script>",
            "insert into role_permission(role_id, permission_id, operator) values ",
            " <foreach collection=\"rolePermissions\" item=\"item\" separator=\",\"> ",
            "  (#{item.roleId}, #{item.permissionId}, #{item.operator})",
            " </foreach> ",
            "</script>"})
    void batchInsert(@Param("rolePermissions") List<RolePermission> rolePermissions);

    /** 删 */
    @Delete("delete from role_permission where role_id = #{roleId}")
    void deleteByRoleId(@Param("roleId") long roleId);

    /** 查 */
    @Select("select permission.* from role_permission right JOIN permission on role_permission.permission_id = permission.id where role_permission.role_id = #{roleId} ")
    List<Permission> findByRoleId(@Param("roleId") long roleId);

    @Select("select count(1) from role_permission where permission_id = #{permissionId}")
    long countByPermissionId(@Param("permissionId") long permissionId);
}